# Prepare CO returns
# Note: clean and rearrange 2020 CO returns
# By Dom Valentino and Chris Kenny
# libs --------------------------------------------------------------------
library(tidyverse) # select(), filter(), mutate(), etc.
library(haven) # read_dta()

# data --------------------------------------------------------------------
returns <- read.csv("../data/returns_co/returns_co.csv")
cvap <- read_dta("../data/modified data/county_cvap.dta") # read cvap data

# prep --------------------------------------------------------------------
final_co <- returns %>% mutate(state = "CO", year = 2020, treat = 1, fips = NA, dem_share_pres = dem_pres / (dem_pres + rep_pres),
                                  dem_share_sen = dem_sen / (dem_sen + rep_sen), dem_share_gov = NA) %>% 
  left_join(., cvap, by = c("state", "county", "year")) %>% 
  mutate(turnout_share = ballots_cast / cvap_approx) %>% 
  select(year, state, county, fips, treat, dem_share_pres, dem_share_sen, dem_share_gov, ballots_cast, cvap, cvap_approx, cvap_moe, turnout_share)
# something is up with broomfield county - doesnt exist in cvap data

# save
write_csv(final_co, path = '../data/analysis/analysis_co.csv')
